//
//  BZResponsePaser.m
//  BZBaseNetWorkSDK
//
//  Created by 瞿衡 on 17/9/24.
//  Copyright © 2017年 瞿衡. All rights reserved.
//

#import "BZResponsePaser.h"
#import "BZBaseInstance.h"
#import "BZTransformNull.h"

const static NSUInteger invalidLogin     = 2000;
const static NSUInteger invalidSessionId = 2008;

@implementation BZResponsePaser


+ (void)handleResponseObject:(id)responseObject
                        task:(NSURLSessionDataTask *)task
                     success:(void(^)(NSString *code , id responseObject))success
                     failure:(void(^)(NSString *code,NSError *error))failure{
    
    responseObject = [BZTransformNull changeType:responseObject];
    
    if ([responseObject isKindOfClass:[NSArray class]]) {
        
        if ([responseObject count]) {
            
            if (success) {
                success(@"200",responseObject);
            }
        }else{
            
            failure(@"",responseObject);
        }
        return ;
        
    }else if ([responseObject isKindOfClass:[NSDictionary class]]){
        
        NSNumber *codeNumber = [responseObject objectForKey:@"code"];
        
        if (codeNumber.integerValue == 0) {
            
            if (success) {
                
                NSDictionary *reponseHeaders = [(NSHTTPURLResponse *)task.response allHeaderFields];
                
                if ([[reponseHeaders objectForKey:@"sessionId"] length] ) {
                    
                    NSMutableDictionary *dict = nil;
                    
                    [BZBaseInstance shareInstance].sessionId = [reponseHeaders objectForKey:@"sessionId"];
                    [BZBaseInstance shareInstance].userId    = [[responseObject objectForKey:@"customerId"]longLongValue]?:0;
                    
                    
                    if ([responseObject count]) {
                        
                        
                        if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
                            
                            dict = [NSMutableDictionary dictionaryWithDictionary:responseObject[@"data"]];
                            [dict setObject:[reponseHeaders objectForKey:@"sessionId"] forKey:@"sessionId"];
                            success(@"200",[dict copy]);
                        }
                        else{
                            success(@"200",responseObject[@"data"]);
                        }
                    }else{
                        dict = [NSMutableDictionary dictionaryWithObject:[reponseHeaders objectForKey:@"sessionId"] forKey:@"sessionId"];
                        success(@"200",[dict copy]);
                    }
                }else{
                    
                    success(@"200",responseObject[@"data"]);
                }
            }
        }else{
            
            NSString *code     = [NSString stringWithFormat:@"%ld",(long)codeNumber.integerValue];
            NSString *message  = responseObject[@"message"];
            //不要改它  涉及app session失效处理和抢占式处理
            if (invalidLogin == [code integerValue]|| invalidSessionId ==[code integerValue]){
                
                    NSString *expireTime = [NSString stringWithFormat:@"%.0f",[[NSDate date]timeIntervalSince1970]];
                //                    [ZZFDataBaseManager updateCurrentUserExpireTimeByParamSync:expireTime
                //                                                                      outError:nil];
                
                NSDictionary* dic = @{@"code":@([code integerValue])};
                [[NSNotificationCenter defaultCenter] postNotificationName:@"Notifi_App_HandleLogin" object:nil userInfo:dic];
            }
            
            // showClueWithMessage([UIApplication  sharedApplication].keyWindow, message);
            if (failure) {
                NSError *error = nil;
                if (message) {
                    error = [NSError errorWithDomain:@"com.cn.zzf"
                                                code:code.integerValue
                                            userInfo:@{NSLocalizedDescriptionKey:message}];
                }
                failure(message,error);
            }
        }
    }
}

@end
